#version 330 core
out vec4 FragColor;
struct Material{    //物体属性
    sampler2D diffuse;
    sampler2D specular;
    float shininess;
};

struct Light{       //光的属性
    vec3 position;
    vec3 direction;
    float cutOff;
    float outerCutOff;
    vec3 ambient;
    vec3 diffuse;
    vec3 specular;

    float constant;
    float linear;
    float quadratic;
};

uniform Material material;
uniform Light light;
uniform vec3 objectColor;
uniform vec3 viewPos;
in vec3 Normal;
in vec3 FragPos;
in vec2 TexCoords;
void main(){
    vec3 lightDir=normalize(light.position-FragPos);
    float theta=dot(-lightDir,normalize(light.direction));
    float epsilon=light.cutOff-light.outerCutOff;
    float intensity=clamp((theta-light.outerCutOff)/epsilon,0.0,1.0);


    //ambient
    vec3 ambient=light.ambient*texture(material.diffuse,TexCoords).rgb;

    float distance=length(light.position-FragPos);
    float attenuation=1.0/(light.constant+light.linear*distance+light.quadratic*distance*distance);

    vec3 norm=normalize(Normal);

    float diff=max(dot(norm,lightDir),0.0);
    vec3 diffuse=diff*light.diffuse*texture(material.diffuse,TexCoords).rgb;

    vec3 viewView=normalize(viewPos-FragPos);

    vec3 reflectDir=reflect(-lightDir,norm);
    float spec=pow(max(dot(viewView,reflectDir),0.0),material.shininess);
    vec3 specular=spec*light.specular*texture(material.specular,TexCoords).rgb;

    diffuse*=intensity;
    specular*=intensity;
    vec3 result=(ambient+diffuse+specular)*attenuation;

    FragColor=vec4(result,1.0);

}
